package com.chen.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_common.pojo.Brand;
import com.chen.shopping_common.service.BrandService;
import com.chen.shopping_goods_service.mapper.BrandMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService //将实现的Dubbo接口注册到Nacos中
@Transactional //事务
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @Override
    public Brand findById(Long id) {
        return brandMapper.selectById(id);
    }

    @Override
    public List<Brand> findAllBrand() {
        return brandMapper.selectList(null);
    }

    @Override
    public void addBrand(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public void deleteBrand(Long id) {
        brandMapper.deleteById(id);
    }


    @Override
    public Page<Brand> findBrandByPage(Brand brand, int page, int size) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(brand != null && StringUtils.hasText(brand.getName())){
            queryWrapper.like("name", brand.getName());
        }
            return brandMapper.selectPage(new Page(page,size),queryWrapper);


    }
}
